<?php
header("Content-type: image/jpeg");

//原始图像
$dst = "images/flower_1.jpg";

//得到原始图片信息
$dst_im = imagecreatefromjpeg($dst);
$dst_info = getimagesize($dst);

//水印图像
$src = "images/logo.gif";
$src_im = imagecreatefromgif($src);
$src_info = getimagesize($src);

//水印透明度
$alpha = 30;

//合并水印图片
imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
$src_info[1],$alpha);

//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);


<!-- 新版本之后imagecopymerge函数几乎不使用了，我们可直接使用imagecopy来生成水印两个函数的功能是完全一样的。 -->

//增加水印
$watermark   =1;
$watertype   =2;
$waterstring =''; 
$waterimg="z.png";    //水印图片
$sFilePath ='aa.jpg';
if($watermark==1)
{
 $image_size = getimagesize($sFilePath); //上传的图片
 $water_size = getimagesize($waterimg);  //水印图片
 $iinfo=getimagesize($sFilePath,$iinfo);
 $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
 $white=imagecolorallocate($nimage,255,255,255);
 $black=imagecolorallocate($nimage,0,0,0);
 $red=imagecolorallocate($nimage,255,0,0);
 imagefill($nimage,0,0,$white);
 switch ($iinfo[2])
 {
  case 1:
   $simage =imagecreatefromgif($sFilePath);
   break;
  case 2:
   $simage =imagecreatefromjpeg($sFilePath);
   break;
  case 3:
   $simage =imagecreatefrompng($sFilePath);
   break;
//            case 6:
//            $simage =imagecreatefromwbmp($sFilePath);
//            break;
  default:
   die("不支持的文件类型");
   exit;
 }
 
 imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
 
 switch($watertype)
 {
  case 1:   //加水印字符串
   imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
   break;
  case 2:   //加水印图片
   $simage1 =imagecreatefrompng($waterimg);     
   $x= $image_size[0]-$water_size[0];
   $y= $image_size[1]-$water_size[1];
   imagecopy($nimage,$simage1,$x,$y,0,0,240,65);
   imagedestroy($simage1);
   break;
 }
    
 switch ($iinfo[2])
 {
  case 1:
   imagegif($nimage, $sFilePath);
//            imagejpeg($nimage, $sFilePath);
   break;
  case 2:
   imagejpeg($nimage, $sFilePath);
   break;
  case 3:
   imagepng($nimage, $sFilePath);
   break;
//            case 6:
//            imagewbmp($nimage, $sFilePath);
//            //imagejpeg($nimage, $sFilePath);
//            break;
 }
 
 //覆盖原上传文件
 imagedestroy($nimage);
 imagedestroy($simage);
}